package com.vishop.daos;

import java.util.Collection;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.vishop.models.Categorie;

@Repository
public class CategorieDaoImpl {

	@Autowired
	private SessionFactory sessionFactory;
	
	public Collection<Categorie> getAll() {
		return sessionFactory.getCurrentSession().createQuery("from Categorie").list();
	}
	
	public Categorie getById(int id){
		return (Categorie) sessionFactory.getCurrentSession().get(Categorie.class, id);
	}
	
	public int save(Categorie category){
		return (Integer) sessionFactory.getCurrentSession().save(category);
	}	

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public Categorie getCategoriebyProduct(int id){
		Query query = sessionFactory.getCurrentSession().createQuery("from Product where categorie.categorieId =:categorieId");
		query.setParameter("categorieId", 1);
		Collection productlijst = query.list();
		return (Categorie) productlijst;
	}
	
	
}
